<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	 
	function __construct()
    {
        // Call the Model constructor 
        parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->library('session');	
		$this->load->database();
		$this->load->library('form_validation');
		$this->load->model('welcome_mdl');
    } 
	public function index()
	{
		if($this->session->userdata('user_data') == false)
		{
			$this->load->view('login_view');
		}
		else
		{
			$data["session"] 			= $this->session->userdata('user_data');
			$data["page_name"]			= "home";
			if($data["session"]["type"] == "teacher")
			{
				redirect("teacher/user");
			}
			$this->load->view('header',$data);
			$this->load->view('learning_center',$data);	
		}
	}
	public function ask_question()
	{
		if($this->session->userdata('user_data') == false)
		{
			$this->load->view('login_view');
		}
		else
		{
			$data["session"] 			= $this->session->userdata('user_data');
			$data["page_name"]		= "ask_question";
			$this->load->view('header',$data);
			$this->load->view('ask_question',$data);	
		}
	}
	public function notification()
	{
		$data["session"] 		= $this->session->userdata('user_data');
		$data["page_name"]			= "notification";
		$this->load->view('header',$data);
		$this->load->view('notification',$data);
	}
	public function question_paper($param1 = null)
	{
		if( $param1 == null)
		{
			redirect("home");
		}
		$data["session"] = $this->session->userdata('user_data');
		$data["page_name"]			= "question_paper";
		$data["paper_id"] = $param1;
		$this->load->view('header',$data);
		$this->load->view('question_paper',$data);
	}
	public function question_paper_report( $param1 = null)
	{
		$data["exam_id"] =  $param1;
		$data["session"] = $this->session->userdata('user_data');
		$data["page_name"]			= "question_paper_report";
		$this->load->view('header',$data);
		$this->load->view('question_paper_report',$data);
	}
	public function add_for_question()
	{
		$subject 	= $_POST["subject"];
		$topic   	= $_POST["topic"];
		$question	= $_POST["question"];
		$key 		= 0;
		$data       = array();
		if( $subject == "")
		{
			$data["error"][$key]["type"] = "subject";
			$data["error"][$key]["msg"] = "You should select a subject";			
			$key++;
			$data["success"] = 0;
		}
		if( $topic == "")
		{
			$data["error"][$key]["type"] = "topic";
			$data["error"][$key]["msg"] = "Topic can not be blank";			
			$key++;
			$data["success"] = 0;
		}
		if( $question == "")
		{
			$data["error"][$key]["type"] = "question";
			$data["error"][$key]["msg"] = "Question can not be blank";			
			$key++;
			$data["success"] = 0;
		}
		if($key == 0)
		{
			$data["success"] = 1;
			$session =  $this->session->userdata("user_data");
			$data_to 				= array();
			$data_to["user_id"] 	= $session["user_id"];
			$data_to["subject"] 	= $subject;
			$data_to["topic"] 		= $topic ;
			$data_to["question"] 	= $question ;
			$this->db->insert("tbl_ask_question",$data_to);			
		}
		echo json_encode($data);
	}
	public function submit_exam()
	{
		$question_types =  $_POST["question_type"];
		$exam_id		=  $_POST["exam_id"];
		$answers		=  $_POST["answer"];
		$questions		=  $_POST["question"];
		foreach($answers as $key => $answer)
		{
			$data_sub 						= 	array();
			$data_sub["exam_id"] 			=	$exam_id ;
			$data_sub["student_id"] 		=	$this->session->userdata("user_data")["user_id"] ;
			$data_sub["question_id"] 		=	$key ;
			$data_sub["question_type"] 		=	$question_types[$key] ;
			$data_sub["question"] 			=	$questions[$key] ;
			$data_sub["answer"] 			=	$answers[$key] ;
			if($question_types[$key] == "multiple")
			{
				$ker = "";
				foreach($data_sub["answer"] as $ans)
				{
					$ker.= $ans.",";
				}
				$data_sub["answer"] = $ker;
				$data_sub["status"] 			=	0 ;
				$this->db->insert("submitted_by_students",$data_sub);
			}
			else
			{
				$data_sub["status"] 			=	0 ;
				$this->db->insert("submitted_by_students",$data_sub);
			}		
		}
		$data_sub 						= array();
		$data_sub["exam_id"] 			=	$exam_id ;
		$data_sub["submitted_by_user"] 	=	$this->session->userdata("user_data")["user_id"];
		$data_sub["status"] 			=	0;
		$this->db->insert("exam_submits",$data_sub);
		$this->session->set_flashdata("good_company",1);
		
		redirect("home");
		//submitted_by_students
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */